package com.tangji.signaltower.websocket.processor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.utils.StringUtils;
import com.tangji.signaltower.enums.BizCodeEnum;
import com.tangji.signaltower.websocket.model.MessageVo;
import com.tangji.signaltower.websocket.model.WebUser;
import com.tangji.signaltower.websocket.session.ServerSession;
import com.tangji.signaltower.websocket.session.SessionFactory;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 消息处理器
 * User: Amarendar
 * Date: 2020/6/2
 * Time: 下午2:17
 */
public class MessageProcessor {
    private static final Logger logger = LoggerFactory.getLogger(MessageProcessor.class);
    /**
     * 单例
     */
    private static MessageProcessor singleInstance = new MessageProcessor();

    public static MessageProcessor instance() {
        return singleInstance;
    }

    /**
     * 收到客户端消息后调用的方法
     *
     * @param ctx
     * @param message
     * @return
     */
    public String onMessage(ChannelHandlerContext ctx, String message) {
        String ret = "success";
        if (message.trim().startsWith("{")) {
            MessageVo messageVo = JSON.parseObject(message, MessageVo.class);
            WebUser user = null;
            BizCodeEnum bizCodeEnum = BizCodeEnum.getEnum(messageVo.getBizCode());
            if (bizCodeEnum == null) {
                return "failure";
            }
            switch (bizCodeEnum) {
                case C0001:

                    JSONObject headObj = JSON.parseObject(messageVo.getHead());
                    JSONObject bodyObj = JSON.parseObject(messageVo.getBody());
                    String token = headObj.getString("token");
                    String deviceId = headObj.getString("deviceId");
                    if (StringUtils.isEmpty(token)) {
                        token = "";
                    }
                    if (StringUtils.isEmpty(deviceId)) {
                        deviceId = "";
                    }

                    String subsc = bodyObj.getString("subscription");
                    logger.info("== subsc8888 ==,subsc:{}", subsc);
                    if (StringUtils.isEmpty(deviceId)) {
                        return "failure";
                    }
                    if (subsc != null && subsc.length() > 0) {
                        String sessionId = StringUtils.isNotEmpty(token) ? token : deviceId;
                        List<String> list = JSON.parseArray(subsc, String.class);

                        //获取已经存在的session
                        ServerSession oldSession = ServerSession.getSession(ctx.channel());

                        ServerSession newServerSession = new ServerSession(ctx.channel(), sessionId)
                                .setWebUser(new WebUser(token, deviceId))
                                .setSubscription(list);

                        //设置行情订阅
                        SessionFactory.updateSession(oldSession, newServerSession);
                        logger.info("session,token,绑定成功,channelId:{},token:{},deviceId:{},sessionId:{},list:{}", newServerSession.getChannel().id().asLongText(), token, deviceId, sessionId, list.toString());
                    }
                    break;
                case C9999:

                    ret = "RETURN_THE_ORIGINAL_DATA";
                    break;
                default:
                    break;
            }
        }
        return ret;
    }

}